package com.lili.str;

/**
 * @Auther: 李 力
 * @Date: 2024/8/22
 * @Description: 最长回文子串
 * @version: 1.0
 */
public class LeetCode5 {
    private static int left;

    private static int right;

    public static String longestPalindrome(String s) {
        left = 0;
        right = 0;
        char[] charArray = s.toCharArray();
        for (int i = 0; i < charArray.length - 1; i++) {
            extend(charArray, i, i);
            extend(charArray, i, i + 1);
        }
        return new String(charArray, left, right - left + 1);
    }

    private static void extend(char[] chars, int i, int j) {
        while (i >= 0 && j < chars.length && chars[i] == chars[j]) {
            i--;
            j++;
        }
        i++;
        j--;
        if (j - i > right - left) {
            left = i;
            right = j;
        }
    }
}
